package com.example.waimaiandroid.comment;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.waimai.HttpUtil.MyPost;
import com.example.waimai.pojo.Orders;
import com.example.waimai.util.MyThreadPool;
import com.example.waimaiandroid.R;

import java.util.HashMap;
import java.util.Map;

public class Comment extends AppCompatActivity {

    private RatingBar riderLevel;
    private RatingBar storeLevel;
    private EditText add_riderContent;
    private EditText add_storeContent;
    private Button postComment;
    private ImageView storeImage;
    private TextView storeName;
    private ImageView back;
    private Orders orders;
    private Integer position;
    private Boolean isFristRider = true;
    private Boolean isFirstStore = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_comment);
        Toolbar toolbar = (Toolbar) findViewById(R.id.commentToolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        initView();
        initData();
        initListener();
    }
    private void initView(){
        back = findViewById(R.id.comment_back);
        riderLevel = findViewById(R.id.riderLevel);
        storeLevel= findViewById(R.id.storeLevel);
        add_riderContent= findViewById(R.id.add_riderContent);
        add_storeContent= findViewById(R.id.add_storeContent);
        postComment= findViewById(R.id.postComment);
        storeImage = findViewById(R.id.comment_StoreImage);
        storeName = findViewById(R.id.comment_storeName);
    }
    private void initData(){
        // 设置数据
        Intent intent = getIntent();
        position = intent.getIntExtra("position",-1);
        orders = (Orders) intent.getExtras().getSerializable("order");
        storeName.setText(orders.getStore().getStoreName());
        Glide.with(this).load(orders.getStore().getStoreImage()).into(storeImage);
    }
    private void initListener(){
        // 设置监听器
        // 返回
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        riderLevel.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                if(isFristRider){
                    // 第一次更改
                    isFristRider = false;
                    add_riderContent.setVisibility(View.VISIBLE);
                }
            }
        });
        storeLevel.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                if(isFirstStore){
                    isFirstStore = false;
                    add_storeContent.setVisibility(View.VISIBLE);
                }
            }
        });
        // 提交
        postComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(riderLevel.getRating() == 0){
                    Toast.makeText(Comment.this,"请评价骑手服务！",Toast.LENGTH_SHORT).show();
                    return ;
                }
                if(storeLevel.getRating() == 0){
                    Toast.makeText(Comment.this,"请评价商家服务！",Toast.LENGTH_SHORT).show();
                    return ;
                }
                // 提交评论
                // update一下
                // 上传评论
                com.example.waimai.pojo.Comment comment = new com.example.waimai.pojo.Comment();
                comment.setOrderId(orders.getId());
                comment.setRiderLevel((int) riderLevel.getRating());
                comment.setStoreLevel((int) riderLevel.getRating());
                String riderContent = add_riderContent.getText().toString();
                String storeContent = add_storeContent.getText().toString();
                comment.setRiderContent(riderContent);
                comment.setStoreContent(storeContent);
                String url = "addComment";
                MyThreadPool.doJob(new Runnable() {
                    @Override
                    public void run() {
                        Boolean isPost = (Boolean) MyPost.doPost(url,comment);
                    }
                });
                Intent data = new Intent();
                data.putExtra("position",position);
                setResult(2,data);
                finish();
            }
        });

    }
}